angular.module('starter.controllers')
.controller('VipCtrl', function($scope, museumFac, $stateParams, $location, $cookies, $ionicScrollDelegate, cfg, $timeout) {
    $scope.change_language = function(id){
      $scope.language = id;
      var expireDate = new Date();  
      expireDate.setDate(expireDate.getDate() + 30);                
      $cookies.put('language', id, {'expires': expireDate.toUTCString()});
      location.reload();
      if(id == 0){
        document.getElementById('title').innerHTML = '提示';
        document.getElementById('windowclosebutton').value = '确定';
      }else{
        document.getElementById('title').innerHTML = 'Hint';
        document.getElementById('windowclosebutton').value = 'Confirm';
      }

    }

    $scope.checkOpenid = function(){
        museumFac.user.usertest(function(data){
          if(data.info == true){
            
          }else{
            $scope.url = cfg.baseUrl + 'home/index/vip';
            $cookies.put('lastPage', $scope.url);
            $location.url('login');
          }
        });

        wx.ready(function(){
          wx.onMenuShareTimeline({
            title: '四方当代美术馆 | 会员中心', // 分享标题
            desc: ' ', // 分享描述
            link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/logo.png', // 分享图标
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
        });
        // 获取“分享给朋友”按钮点击状态及自定义分享内容接口
        wx.onMenuShareAppMessage({
            title: '四方当代美术馆 | 会员中心',  // 分享标题
            desc: ' ', // 分享描述
            link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/logo.png', // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
          });
        });
    }
    $scope.checkOpenid();


  $scope.getLanguage = function(){
    $scope.language = $cookies.get('language');
    if($scope.language){
        $scope.language = $scope.language;
    }else{
        $scope.language = 0;
    }
    if($scope.language == 0){
      document.getElementById('title').innerHTML = '提示';
      document.getElementById('windowclosebutton').value = '确定';
    }else{
      document.getElementById('title').innerHTML = 'Hint';
      document.getElementById('windowclosebutton').value = 'Confirm';
    }
  }
  $scope.getLanguage();
  
  $scope.gotoTicket = function(){
  	$location.url('my_ticket');
  }

  $scope.gotoInfo = function(){
  	$location.url('info');
  }

  $scope.gotoQrcode = function(){
  	$location.url('qrcode');
  }

  $scope.backhome = function(){
	  $location.url('home');
  }

  $scope.gotoOrder = function(){
    $location.url('order');
  }

  $scope.checkOpenid = function(){
	museumFac.user.usertest(function(data){
        $scope.is_login = data.info;
      },function(data){
        alert(data.data.info);
    });
  }
  $scope.checkOpenid();

  $scope.gotoCenter = function(){
  	if($scope.is_login == true){
  		$location.url('center');
  	}else{
      $scope.url = window.location.href;
      $cookies.put('lastPage', $scope.url);
  		$location.url('login');
  	}	
  }

  $scope.getUser = function() {
    museumFac.user.profile(function(data){
      $scope.user = data.info;
    },function(data){
      alert(data.data.info);
    });
  };
  $scope.getUser();

  $scope.getVipTypes = function(){
    museumFac.order.viptypes(function(data){
      $scope.viptypes = data.info;
      // $scope.vip_title = data.identity;
    },function(data){
      alert(data.data.info);
    });
  }
  $scope.getVipTypes();

  $scope.changeMore = function(list){
    // $ionicScrollDelegate.$getByHandle('mainScroll').resize();
    if(list.seemore == 1){
      list.seemore = 0;
      $ionicScrollDelegate.$getByHandle('mainScroll').resize();
    }else{
      list.seemore = 1;
      $ionicScrollDelegate.$getByHandle('mainScroll').resize();
    }
    $timeout(function() {
      $ionicScrollDelegate.$getByHandle('mainScroll').resize();
    }, 100);
  }

  $scope.checkVip = function(type){
    window.location.href = cfg.baseUrl + 'home/index/vipcheckpay?type=' + type.id;
  }

  $scope.create_card = function(flag){
    switch(flag){
      case 'student': var title = '学生年卡';break;
      case 'adult': var title = '成人年卡';break;
      case 'family': var title = '家庭年卡';break;
      default: return;
    }
    var card = {};
    card = {
      "card": {
        "card_type": "MEMBER_CARD",
        "member_card": {
            "background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/X1O7AFSEgK0TtlLSZRES2CO7QBJxhIxRltUYVicdOlicia9k7kMh2GNhCf00vDxu6vhS8oaiaANiafHT2XbGN3Iz8Ww/0",
            "base_info": {
                "logo_url": "http://mmbiz.qpic.cn/mmbiz_png/X1O7AFSEgK0TtlLSZRES2CO7QBJxhIxRbU6tBGhd8giaaYscZCLtnUx2dVx3YxuFcjKKhpClxfqk5kXDK74xicyg/0",
                "brand_name": "四方当代美术馆",
                "code_type": "CODE_TYPE_BARCODE",
                "title": title,
                "color": "Color060",
                "notice": "使用时向服务员出示此卡",
                "description": "会员权益生效日即会员登记信息日，有效期一年以内（具体以公众号平台为准，微信卡包内标注的永久有效非实际有效期）。会员有效期内凭会员码入园参观。",
                "date_info": {
                    "type": "DATE_TYPE_PERMANENT"//表示永久有效
                },
                "sku": {
                    "quantity": 50000000
                },
                "can_share":false,
                "get_limit": 1,
                "use_custom_code": true,
                "can_give_friend": false,
                "need_push_on_view": false
            },
            "supply_bonus": false,
            "supply_balance": false,
            "prerogative": "展览：\n免费参观全年艺术展览与四方当代艺术湖区全景（不含建筑内部）\n特邀参加美术馆新展览开幕酒会\n优先参与和四方当代美术馆合作的展览及其他艺文活动\n可免费携带同伴一名（学生年卡同伴仅限学生）\n活动：\n每年不少于4次的美术馆会员专属活动\n（参观艺术家工作室，高端brunch，馆长、艺术家或策展团队的会员专场导览等）\n优先预约并参与全年的活动和讲座\n学生卡及成人卡可优惠参加美术馆收费活动（指定活动及亲子活动除外）\n家庭卡可优惠参加美术馆收费活动（两位小朋友可同时享受优惠，指定活动除外）\n其他：\n美术馆咖啡店消费享受9折优惠\n美术馆商店书籍/商品享有9折优惠（部分商品除外，详情请询问店员）\n定期收到会员专属资讯",
            "auto_activate": true
        }
      }
    };
    museumFac.wechat.addtowallet({
        data: card
    },function(data){
        $scope.posturl = data.info;
        console.log(data.info);
    },function(data){
        alert(data.data.info);
    });
  }

  $scope.uploadWechatImg = function(){
    $scope.editcard = {};
    $scope.new_vip = {};
  }

  $scope.edit_card = function(){
    if(!$scope.editcard['cardId']){
      return;
    }
    var card = {};
    card = {
      "card_id": $scope.editcard['cardId'],
      "member_card": {
        "base_info": {
          "description": "会员权益生效日即会员购买日，有效期一年以内（具体以公众号平台为准，微信卡包内标注的永久有效非实际有效期）。会员有效期内凭会员码入园参观。"
        }
        // "prerogative": $scope.editcard['prerogative']
      }
    };
    museumFac.wechat.editwechatcard({
        data: card
    },function(data){
        $scope.posturl = data.info;
        console.log(data.info);
    },function(data){
        alert(data.data.info);
    });
  }

  $scope.delete_card = function(codeid){
    museumFac.wechat.deletecwxcard({
      code: codeid
    },function(data){
      console.log(data.info);
    },function(data){
      console.log(data.data.info);
    });
  }

  $scope.add_card = function(){
    museumFac.wechat.wxgetticket(function(data){
      wx.addCard({
        cardList: [{
          cardId: data.card_id,
          cardExt: data.info
        }], // 需要添加的卡券列表
        success: function (res) {
          var cardList = res.cardList; // 添加的卡券列表信息
          console.log(res);
        },
        error:function(res){
          console.log(res);
        }
      });
    },function(data){
      alert(data.data.info);
    });
  }

  $scope.add_vipmember = function(){
    museumFac.wechat.addvipmember($scope.new_vip,function(data){
      alert(data.info);
    },function(data){
      alert(data.data.info);
    });
  }
})